package community.service.impl;

import java.util.ArrayList;

import community.dal.DatabaseSession;
import community.dao.ICommentDao;
import community.dao.IQuestionDao;
import community.dao.impl.CommentDao;
import community.dao.impl.QuestionDao;
import community.domain.Question;
import community.exception.DatabaseException;
import community.service.IQuestionService;

public class QuestionService implements IQuestionService {
	IQuestionDao questionDao = new QuestionDao();
	ICommentDao commentDao = new CommentDao();

	@Override
	public void addQuestion(Question question) throws DatabaseException {
		try (DatabaseSession databaseSession = new DatabaseSession()) {
			questionDao.addQuestion(databaseSession, question);
		}
	}

	@Override
	public ArrayList<Question> getQuestion(int tid) throws DatabaseException {
		try (DatabaseSession databaseSession = new DatabaseSession()) {
			ArrayList<Question> questions = questionDao.getQuestion(databaseSession, tid);
			for (Question question : questions) {
				question.setComments(commentDao.getFirstLayerCommentByQuestionID(databaseSession, question.getQid()));
			}
			return questions;
		}
	}


	@Override
	public Question getQuestionByqid(int qid) throws DatabaseException {
		Question question = null;
		try (DatabaseSession databaseSession = new DatabaseSession()) {
			question = questionDao.getQuestionByqid(databaseSession, qid);			
		}
		return question;
	}

}
